<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表号" prop="meterNo">
        <el-input
          v-model="queryParams.meterNo"
          placeholder="请输入表号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产厂家" prop="maker" v-if="queryParams.meterType == '0'">
        <el-select v-model="queryParams.maker" placeholder="请选择生产厂家" clearable>
          <el-option v-for="dict in dict.type.maker" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['meterread:waterData:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['meterread:waterData:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['meterread:waterData:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5" v-if="false">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['meterread:waterData:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table v-loading="loading" :data="waterDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="户号" align="center" prop="userId" />
      <el-table-column label="户名" align="center" prop="userName" />
      <el-table-column label="地址" align="center" prop="waterAddress" />
      <el-table-column label="表号" align="center" prop="meterNo" />
      <el-table-column label="阀状态" align="center" prop="controlStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.control_status" :value="scope.row.controlStatus" />
        </template>
      </el-table-column>
       <el-table-column label="表读数" align="center" prop="currentReading" />
       <el-table-column label="表底数" align="center" prop="initialReading" />
<!--      <el-table-column label="瞬时流量(m³/h)" align="center" prop="flux" />
      <el-table-column label="压力(kPa)" align="center" prop="pressure" />-->
       <el-table-column label="抄表时间" align="center" prop="readDate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleInfo(scope.row)"
            v-hasPermi="['meterread:waterData:hisList']"
          >历史数据</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水抄历史数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表号" prop="meterNo">
          <el-input v-model="form.meterNo" placeholder="请输入表号" />
        </el-form-item>
        <el-form-item label="户号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入户号" />
        </el-form-item>
        <el-form-item label="累计用量" prop="cumulativeFlow">
          <el-input v-model="form.cumulativeFlow" placeholder="请输入累计用量" />
        </el-form-item>
        <el-form-item label="压力" prop="pressure">
          <el-input v-model="form.pressure" placeholder="请输入压力" />
        </el-form-item>
        <el-form-item label="瞬时流量" prop="flowRate">
          <el-input v-model="form.flowRate" placeholder="请输入瞬时流量" />
        </el-form-item>
        <el-form-item label="流速" prop="flowVelocity" v-if="false">
          <el-input v-model="form.flowVelocity" placeholder="请输入流速" />
        </el-form-item>
        <el-form-item label="抄表日期" prop="readDate">
          <el-date-picker clearable
                v-model="form.readDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择通抄表日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <image-upload v-model="form.picture" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查询抄表数据对话框 -->
    <el-dialog :title="title" :visible.sync="dataOpen" width="1300px" append-to-body>
      <el-table v-loading="hisLoading" :data="waterDataHisList">
        <el-table-column type="selection" width="55" align="center" v-if="false"/>
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{(queryHisParams.pageNum - 1) * queryHisParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="户号" align="center" prop="userId" width="100px"/>
        <el-table-column label="户名" align="center" prop="userName" width="160px"/>
        <el-table-column label="地址" align="center" prop="waterAddress" show-overflow-tooltip/>
        <el-table-column label="表号" align="center" prop="meterNo" />
        <el-table-column label="阀状态" align="center" prop="controlStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.control_status" :value="scope.row.controlStatus" />
          </template>
        </el-table-column>
        <el-table-column label="本次表读数" align="center" prop="currentReading" />
        <el-table-column label="上次表读数" align="center" prop="lastReading" />
        <el-table-column label="压力" align="center" prop="pressure" />
        <el-table-column label="瞬时流量" align="center" prop="flowRate" />
        <el-table-column label="抄表时间" align="center" prop="readDate" width="160"/>
      </el-table>
      <pagination
        v-show="hisTotal>0"
        :total="hisTotal"
        :page.sync="queryHisParams.pageNum"
        :limit.sync="queryHisParams.pageSize"
        @pagination="getHisList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listWaterData, getWaterData, listWaterDataHis, addWaterData, updateWaterData } from "@/api/meterread/waterData";

export default {
  name: "WaterData",
  dicts: ['control_status','maker'],
  data() {
    return {
      // 遮罩层
      loading: true,
      hisLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      hisTotal: 0,
      // 抄表数据表格数据
      waterDataList: [],
      waterDataHisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dataOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        meterNo: null,
        meterType: null,
        userId: null,
        userName: null,
        readDate: null,
        maker: null,
      },
      queryHisParams: {
        pageNum: 1,
        pageSize: 10,
        meterNo: null,
        meterType: null,
        userId: null,
        readDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // 表单校验
      rules: {
        // meterNo: [
        //   { required: true, message: "表号不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    const meterType = this.$route.query && this.$route.query.meterType;
    this.queryParams.meterType = meterType;
    this.getList();
    this.getHisList();
  },
  methods: {
    /** 查询抄表数据列表 */
    getList() {
      this.loading = true;
      listWaterData(this.queryParams).then(response => {
        this.waterDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getHisList() {
      this.hisLoading = true;
      listWaterDataHis(this.queryHisParams).then(response => {
        this.waterDataHisList = response.rows;
        this.hisTotal = response.total;
        this.hisLoading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        meterNo: null,
        userId: null,
        instantaneousFlow: null,
        cumulativeFlow: null,
        readDate: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抄表数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.queryParams.meterNo = row.meterNo;
      listWaterData(this.queryParams).then(response => {
        this.waterDataList = response.rows;
        this.total = response.total;
        this.open = true;
        this.title = "抄表历史数据";
      });
    },
    /** 抄表历史数据 */
    handleInfo(row) {
      this.queryHisParams.pageNum = 1;
      this.queryHisParams.pageSize = 10;
      this.queryHisParams.meterNo = row.meterNo;
      this.queryHisParams.meterType = row.meterType;
      listWaterDataHis(this.queryHisParams).then(response => {
        this.waterDataHisList = response.rows;
        this.hisTotal = response.total;
        this.dataOpen = true;
        this.title = "抄表历史数据";
      });
    },
    field101BeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWaterData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.meterType = this.queryParams.meterType;
            addWaterData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除抄表数据编号为"' + ids + '"的数据项？').then(function() {
        return delWaterData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('meterread/waterData/export', {
        ...this.queryParams
      }, `waterData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
